package com.shujia

import org.apache.flink.configuration.Configuration
import org.apache.flink.runtime.state.StateBackend
import org.apache.flink.runtime.state.filesystem.FsStateBackend
import org.apache.flink.streaming.api.CheckpointingMode
import org.apache.flink.streaming.api.environment.CheckpointConfig.ExternalizedCheckpointCleanup
import org.apache.flink.streaming.api.scala.StreamExecutionEnvironment
import org.apache.flink.table.api.EnvironmentSettings
import org.apache.flink.table.api.bridge.scala.StreamTableEnvironment
import org.apache.flink.table.catalog.hive.HiveCatalog

abstract class FlinkTool {

  var bsTableEnv: StreamTableEnvironment = _

  def main(args: Array[String]): Unit = {
    /**
      * 创建flink环境
      *
      */

    val bsEnv: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment


    /**
      * 开启checkpoint
      *
      */

    // 每 1000ms 开始一次 checkpoint
    bsEnv.enableCheckpointing(10000)

    // 高级选项：

    // 设置模式为精确一次 (这是默认值)
    bsEnv.getCheckpointConfig.setCheckpointingMode(CheckpointingMode.EXACTLY_ONCE)

    // 确认 checkpoints 之间的时间会进行 500 ms
    bsEnv.getCheckpointConfig.setMinPauseBetweenCheckpoints(500)

    // Checkpoint 必须在一分钟内完成，否则就会被抛弃
    bsEnv.getCheckpointConfig.setCheckpointTimeout(60000)

    // 同一时间只允许一个 checkpoint 进行
    bsEnv.getCheckpointConfig.setMaxConcurrentCheckpoints(1)

    //当任务取消的时候是否保留checkpoint,默认不保留
    bsEnv.getCheckpointConfig.enableExternalizedCheckpoints(ExternalizedCheckpointCleanup.RETAIN_ON_CANCELLATION)
    /**
      * 指定保存状态的位置
      *
      */
    //文件系统的状态后端，可以说hdfs
    val stateBackend: StateBackend = new FsStateBackend("hdfs://master:9000/data/flink/gma/checkpoint")
    bsEnv.setStateBackend(stateBackend)


    //flink sql 配置文件对象
    val bsSettings: EnvironmentSettings = EnvironmentSettings
      .newInstance()
      .useBlinkPlanner() //计划器
      .inStreamingMode() //流模式
      .build()

    bsTableEnv = StreamTableEnvironment.create(bsEnv, bsSettings)

    val configuration: Configuration = bsTableEnv.getConfig.getConfiguration
    //设置动态option
    configuration.setString("table.dynamic-table-options.enabled", "true")

    /**
      * 注册hive catalog
      *
      */

    val name = "myhive"
    val defaultDatabase = "default"
    val hiveConfDir = "/usr/local/soft/hive-1.2.1/conf/" // 集群路径

    val hive = new HiveCatalog(name, defaultDatabase, hiveConfDir)

    //注册hive catalog
    bsTableEnv.registerCatalog("myhive", hive)

    //切换catalog
    bsTableEnv.useCatalog("myhive")


    //调用子类的run方法
    run(args)

  }

  def run(args: Array[String])

}
